﻿using System;

namespace CoreLibrary.Date {

    public class DateUtil {

        public static readonly DateTime First    = new DateTime(0001, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        public static readonly DateTime Epoch    = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        public static readonly DateTime Year2050 = new DateTime(2050, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        public static DateTime ConvertMillisSinceEpochToDateTime(long milliseconds) {
            return Epoch.Add(TimeSpan.FromMilliseconds(milliseconds)).ToLocalTime();
        }

        public static DateTime ConvertMillisSinceEpochToDateTime(long? milliseconds, DateTime returnIfMsIsNull) {
            return milliseconds == null ? returnIfMsIsNull : ConvertMillisSinceEpochToDateTime(milliseconds.Value);
        }

        public static long ConvertDateTimeToMillisSinceEpoch(DateTime dateTime) {
            var utc = dateTime.ToUniversalTime();
            return utc.Ticks / 10000 - Epoch.Ticks / 10000;
        }

    }

}